//
//  ChainMineVC.m
//  Chain
//
//  Created by zhaosh on 2018/4/3.
//  Copyright © 2018年 zhaosh. All rights reserved.
//
#import "ChainMineVC.h"
#import "ChainLoginVC.h"
#import "ChainMineTableCell.h"
#import "ChainMineItem.h"
#import "UIImage+Color.h"
#import "ChainAccountSetController.h"
#import "ChainCollectedVC.h"

@interface ChainMineVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UITableView *myTableView;
@end

@implementation ChainMineVC
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIViewController *vc = [self.navigationController.viewControllers lastObject];
        if (vc.fd_prefersNavigationBarHidden) {
            [self.navigationController setNavigationBarHidden:YES animated:NO];
        } else {
            [self.navigationController setNavigationBarHidden:NO animated:NO];
        }
    });
    
    if (!SESSION_ID  && !USER_ID) {
        ChainLoginVC *vc=[[ChainLoginVC alloc] init];
        [self.navigationController pushViewController:vc animated:NO];
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
 
    self.fd_prefersNavigationBarHidden=YES;

    NSArray *tempArr=[NSArray arrayWithObjects:@{@"tileName": @"我的收藏"},@{@"tileName" : @"系统设置"},nil];
    for (NSDictionary *dic in tempArr) {
         ChainMineItem *item =[ChainMineItem yy_modelWithDictionary:dic];
         [self.dataArray addObject:item];
    }
    [self addTableView];
}

- (void)addTableView {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.myTableView];
    self.myTableView.scrollEnabled =NO;
    
    [self setTableHeaderView];
}

-(void)setTableHeaderView{
    UIView *tableHeaderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 180)];
    tableHeaderView.backgroundColor =HEXCOLOR(@"#333333");
    
    UIImageView *headImgView=[[UIImageView alloc] initWithFrame:CGRectMake(30,55, 80, 80)];
    headImgView.layer.cornerRadius=40;
    headImgView.image =[UIImage imageNamed:@"default_headerImg"];
    headImgView.backgroundColor=[UIColor whiteColor];
    [tableHeaderView addSubview:headImgView];
    
    UILabel *accountNameLb = [[UILabel alloc]initWithFrame:CGRectMake(headImgView.right+10, 85, 260, 18)];
    accountNameLb.textAlignment = NSTextAlignmentLeft;
    accountNameLb.textColor = [UIColor whiteColor];
    accountNameLb.font =[UIFont boldSystemFontOfSize:16.0];
    [tableHeaderView addSubview:accountNameLb];
   
    NSString *nickName = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"];
    if (!nickName) {
        nickName=@"无";
    }
    accountNameLb.text  = [NSString stringWithFormat:@"%@",nickName];
    
    UIImageView *circleView=[[UIImageView alloc] initWithFrame:CGRectMake(self.view.width-140, 30, 125, 137)];
    circleView.image=[UIImage imageNamed:@"mine_circle"];
    [tableHeaderView addSubview:circleView];
    
    [self.myTableView setTableHeaderView:tableHeaderView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"AccountCell";
    ChainMineTableCell *cell = (ChainMineTableCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[ChainMineTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.item=self.dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  65;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row==0) {
        ChainCollectedVC *accountSetVC=[[ChainCollectedVC alloc] init];
        [self.navigationController pushViewController:accountSetVC animated:YES];
    }else if (indexPath.row==1){
        ChainAccountSetController *accountSetVC=[[ChainAccountSetController alloc] init];
        [self.navigationController pushViewController:accountSetVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
